export function testGlobal(data:string) {
    console.log('全局挂在方法====',data)
}


export function isEmpty(data:any){
  return data === null || data === undefined || data === ""
};

export function isEmptyArray(data:any){
   return  Array.isArray(data) ? data.length === 0 : true;
};

/**
 * 通用js方法封装处理
 * Copyright (c) 2022
 */

// 日期格式化
export function parseTime(time:any, pattern?:string) {
    if (arguments.length === 0 || !time) {
      return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
        time = parseInt(time)
      } else if (typeof time === 'string') {
        time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
      }
      if ((typeof time === 'number') && (time.toString().length === 10)) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj:any = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result:string, key:string) => {
      let value = formatObj[key]
      // Note: getDay() returns 0 on Sunday
      if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
      if (result.length > 0 && value < 10) {
        value = '0' + value
      }
      return value || 0
    })
    return time_str
  } 
 
  /**
   * 转换字符串，undefined,null等转化为""
   * @param str  字符串
   * @returns  字符串
   */
  export function parseStrEmpty(str:string) {
    if (!str || str == "undefined" || str == "null") {
      return "";
    }
    return str;
  }
      
  /**
   * 判断对象中某个kay是否存在
   * const imageList = { 'hi': hi, 'codepower': codepower, 'experience': experience, 'designpower': designpower }
   * const key = 'hi' 
   * if (!isValidKey(key, imageList)) { // 先进行判断，之后就不会报错了
   *    throw Error('invalid sequence');
   * }
   */
  export function isValidKey(key: string | number | symbol , object: object): key is keyof typeof object {
    return key in object;
  }
  /**
 * 指定时间转换为时间戳
 * @param dateString 时间
 */
   export function toTimeStamp(dateString:string){
  // dateString例如:'2022-03-05'
  // 例如返回:1646611200000
  return new Date(dateString).getTime();
}
    /**
   * 根据自增数生成64进制id
   * @returns 64进制id字符串
   */
  export function idGenerator() {
      let qutient = toTimeStamp((new Date()).toString()) - toTimeStamp('2022-05-01');
      qutient += Math.ceil(Math.random() * 1000); // 防止重複
      const chars ="0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz";
      const charArr = chars.split("");
      const radix = chars.length;
      const res = [];
      do {
        let mod = qutient % radix;
        qutient = (qutient - mod) / radix;
        res.push(charArr[mod]);
      } while (qutient);
      return res.join("");
    }